gtk-demo: Work harder to filter the tree
authorMatthias Clasen <mclasen@redhat.com>
Sun, 12 Jul 2020 22:24:04 +0000 (18:24 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 12 Jul 2020 22:26:56 +0000 (18:26 -0400)
Arrange for an item to be visible if it either
matches the filter or has children that do.

demos/gtk-demo/main.c

index 5bad5e80aa735a36b05f299d51dab8d3f450fe16..880dcbaf1bab720585b8392e90efc99af359bf83 100644 (file)
@@ -965,12 +965,33 @@ selection_cb (GtkSingleSelection *sel,
   gtk_window_set_title (GTK_WINDOW (toplevel), demo->title);
 }
 
+static gboolean
+filter_demo (GtkDemo *demo)
+{
+  int i;
+
+  /* Show only if the name maches every needle */
+  for (i = 0; search_needle[i]; i++)
+    {
+      if (!demo->title)
+        return FALSE;
+
+      if (g_str_match_string (search_needle[i], demo->title, TRUE))
+        continue;
+
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
 static gboolean
 demo_filter_by_name (GtkTreeListRow     *row,
                      GtkFilterListModel *model)
 {
+  GListModel *children;
   GtkDemo *demo;
-  guint i;
+  guint i, n;
 
   /* Show all items if search is empty */
   if (!search_needle || !search_needle[0] || !*search_needle[0])
@@ -979,22 +1000,28 @@ demo_filter_by_name (GtkTreeListRow     *row,
   g_assert (GTK_IS_TREE_LIST_ROW (row));
   g_assert (GTK_IS_FILTER_LIST_MODEL (model));
 
-  demo = gtk_tree_list_row_get_item (row);
-  g_assert (GTK_IS_DEMO (demo));
-
-  /* Show only if the name maches every needle */
-  for (i = 0; search_needle[i]; i++)
+  children = gtk_tree_list_row_get_children (row);
+  if (children)
     {
-      if (!demo->title)
-        return FALSE;
-
-      if (g_str_match_string (search_needle[i], demo->title, TRUE))
-        continue;
+      n = g_list_model_get_n_items (children);
+      for (i = 0; i < n; i++)
+        {
+          demo = g_list_model_get_item (children, i);
+          g_assert (GTK_IS_DEMO (demo));
 
-      return FALSE;
+          if (filter_demo (demo))
+            {
+              g_object_unref (demo);
+              return TRUE;
+            }
+          g_object_unref (demo);
+        }
     }
 
-  return TRUE;
+  demo = gtk_tree_list_row_get_item (row);
+  g_assert (GTK_IS_DEMO (demo));
+
+  return filter_demo (demo);
 }
 
 static void